<?php
/**
 * 获得模型的列表
 *
 * @param String $pTable 数据表名
 * @param String $pConn 条件 L=查询条数 &OB=排序 &cid=分类ID &status=_NO_(可以不查询某字段) &字段=值
 * @return array
 */
function getlist($pTable, $pConn, &$pPage = false)
{
	parse_str($pConn, $tConn);
	//查询条数
	$tLimit = isset($tConn['L'])? $tConn['L']: 10;
	//排序
	$tOrder = isset($tConn['OB'])? $tConn['OB']: 'id desc';
	//默认状态条件
	unset($tConn['L'], $tConn['OB'], $tConn['NP']);
	//Where 条件
	$tWhere = array();
	foreach ($tConn as $k1 => $v1) if('_NO_' !== $v1){
		if(0 === strpos($k1, 'SQL')){
			$tWhere[] = str_replace('\\', '', $v1);
		}
		else{
			$tWhere[] = 0===strpos($v1, 'IN')? "$k1 $v1": "$k1 = '$v1'";
		}
	}
	$tWhere = join(' AND ', $tWhere);
	//不带分页查询
	is_string($pTable) && $pTable = D($pTable);
	if(false === $pPage){
		return $pTable->where($tWhere)->order($tOrder)->limit($tLimit)->select();
	}
	//带分页返回结果
	if(!$tCnt = $pTable->where($tWhere)->count()) return;
	$tPage = getPage($tCnt, $tLimit);
	$pPage = $tPage->show();
	return $pTable->where($tWhere)->order($tOrder)->limit($tPage->firstRow.','.$tPage->listRows)->select();
}
function getPage($pCnt, $pLimit){
	import("ORG.Util.Page");
	$tPage = new Page($pCnt, $pLimit);
	$tPage->setConfig('theme', '%totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first%%linkPage% %end%');
	$tPage->setConfig('header', '条记录');
	$tPage->setConfig('prev', '上一页');
	$tPage->setConfig('next', '下一页');
	$tPage->setConfig('first', '首页');
	$tPage->setConfig('last', '尾页');
	return $tPage;
}
/**
 * 表单数据过滤
 * @param array $pForm POST 或 GET
 */
function filterForm(&$pForm){
	foreach ($pForm as $k1 => $v1){
		if(is_array($v1)) foreach ($v1 as $k2 => $v2){
			filterInput($pForm[$k1][$k2]);
		}
		else{
			filterInput($pForm[$k1]);
		}
	}
}

/**
 * 单值过滤
 * @param string $pVal
 */
function filterInput(&$pVal){
	if(empty($pVal)) return ;
	foreach (array("\\", '"', "'", '<', 'javascript') as $v1)
	if(false !== strpos($pVal, $v1)){
		$pVal = str_replace($v1, '', $pVal);
	}
	return $pVal;
}

/**
 * 返回激励奖金%比
 */
function precent($exp){
	if($exp>1000){
		return 100;
	}else{
		return floor($exp/100)*10;
	}
}

/**
 * 发送邮件
 *
 * @param string $pAddress
 * @param string $pSubject
 * @param string $pBody
 * @return bool
 */
function mailto($pAddress, $pSubject, $pBody){
	Vendor('Mail.PHPMailer');
	$mail = new PHPMailer();
	$mail->IsSMTP();
	$mail->CharSet = 'utf-8';
	$mail->Host = "smtp.exmail.qq.com";
	$mail->SMTPAuth = true;
	$mail->Username = "392235020"; // 邮局用户名
	$mail->Password = "jesuswxy"; // 邮局密码
	$mail->From = "392235020@qq.com"; //发送者邮箱
	$mail->FromName = "粉喜欢";
	$mail->IsHTML(true);
	$mail->AddAddress($pAddress, "");//收件人地址
	$mail->Subject = $pSubject;
	$mail->Body = $pBody;
	return $mail->Send();
}

?>